/**
 * 
 */
package com.tvshows.front.view.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

import com.tvshows.front.factory.DialogFactory;
import com.tvshows.front.factory.TypeUI;
import com.tvshows.front.ui.FrontUI;
import com.tvshows.front.ui.dialog.ActionsUI;
import com.tvshows.front.ui.dialog.DialogUI;
import com.tvshows.front.ui.recap.UpsideCompositeRecapUI;

/**
 * @author tMidy
 * <p>
 * Implementation of {@link ActionsUI}
 * </p>
 */
public class ActionsView
    implements ActionsUI
{

    /**
     * The shell
     */
    private Shell shell;

    /**
     * sets the modify mode
     */
    private boolean modify;

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#openShell(com.tvshows.front.ui.FrontUI)
     */
    @Override
    public void openShell( FrontUI view )
    {
        shell = new Shell( view.getShell() );
        DialogAddFavoriteView frame = new DialogAddFavoriteView( shell, SWT.BORDER | SWT.RESIZE );
        frame.setModify( modify );
        frame.setSize( 330, 280 );
        configureShell( frame );
    }

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#openMenuItem(com.tvshows.front.ui.FrontUI,
     * com.tvshows.front.factory.TypeUI)
     */
    @Override
    public void openMenuItem( FrontUI view, TypeUI typeUI )
    {
        DialogUI dialog = null;

        if ( typeUI == TypeUI.LOG_VIEW )
        {
            shell = new Shell( view.getShell() );
            dialog = DialogFactory.createDialogUI( TypeUI.LOG_VIEW, shell );
        }

        if ( typeUI == TypeUI.OPTION_VIEW )
        {
            shell = new Shell( view.getShell() );
            dialog = DialogFactory.createDialogUI( TypeUI.OPTION_VIEW, shell );
        }
        if ( dialog != null )
        {
            configureShell( dialog );
            shell.pack();
        }
    }

    /**
     * Configures the shell
     * 
     * @param view the view
     */
    private void configureShell( DialogUI view )
    {
        shell.setSize( view.getSize() );

        /** Center the shell **/
        Monitor primary = shell.getDisplay().getPrimaryMonitor();
        Rectangle bounds = primary.getBounds();
        Rectangle rect = shell.getBounds();

        int x = bounds.x + ( bounds.width - rect.width ) / 2;
        int y = bounds.y + ( bounds.height - rect.height ) / 2;

        shell.setLocation( x, y );
        shell.open();
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#getShell()
     */
    @Override
    public Shell getShell()
    {
        return shell;
    }

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#setModify(boolean)
     */
    @Override
    public void setModify( boolean modify )
    {
        this.modify = modify;
    }

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#openJobRunningPopUp(com.tvshows.front.ui.FrontUI)
     */
    @Override
    public void openJobRunningPopUp( FrontUI view )
    {
        openBox( JOB_RUNNING, view.getShell() );
    }

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#openPopUpNoSoftwareFound(com.tvshows.front.ui.recap.UpsideCompositeRecapUI)
     */
    @Override
    public void openPopUpNoSoftwareFound( UpsideCompositeRecapUI upsideUI )
    {
        openBox( NO_SOFT_DEFINED, upsideUI.getShell() );
    }

    /**
     * @see com.tvshows.front.ui.dialog.ActionsUI#openPopUpConfigurationProblem(com.tvshows.front.ui.dialog.DialogUI)
     */
    @Override
    public void openPopUpConfigurationProblem( DialogUI dialogOptionsView )
    {
        openBox( CONFIGURATION_ERROR, dialogOptionsView.getShell() );
    }

    /**
     * Opens a Pop-up with the correct message
     * 
     * @param message the message to show
     * @param shell the shell
     */
    private void openBox( String message, Shell shell )
    {
        MessageBox box = new MessageBox( shell, SWT.ICON_ERROR );
        box.setMessage( message );

        box.open();
    }

    @Override
    public void initFormData()
    {
        // unused
    }

    @Override
    public void initComposites()
    {
        // unused

    }

}
